<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>ws测试</title>

</head>
<body>
{{test()}}
<main>

<div class="left">
        <input type="text" id="text">
        <button id="button-send">发送</button>
 </div>

    <div class="right">
        <div id="content"></div>
 </div>
</main>
<script>

function show_message(text){
    document.getElementById('content').innerText+='服务器发来的消息: '+ text +'\n';
}

/*
if (!('WebSocket' in window)){
    alert('您的浏览器不支持 WebSocket!');
}
*/
var ws = new WebSocket('ws://localhost:5501/wstest_entrance');
ws.onopen = function(){
    console.log('web socket 已连接');
}
ws.onmessage = function (event){
    r_data = event.data; //接收到的数据
    console.log(r_data);
    show_message(r_data);
}
ws.onclose = function(){
    //alert('连接已关闭...');
}

document.getElementById('button-send').onclick = function(event){
    var text =  document.getElementById('text').value ;
    ws.send( text);
    document.getElementById('content').innerText += '我: ' + text +'\n';

}


</script>
</body>
</html>